﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcAppDemo.Models;

namespace MvcAppDemo.Controllers
{
    public class DinnersController : Controller
    {
        IDinnerRepository dinnerRepository;
        public DinnersController()
            : this(new DinnerRepository())
        {
        }
        public DinnersController(IDinnerRepository repository)
        {
            dinnerRepository = repository;
        }
        //
        // GET: /Dinners/
        //public ActionResult Index()
        //{
        //    var dinners = dinnerRepository.FindAllDinners().ToList();
        //    return View("Index", dinners);
        //}
        //
        // GET: /Dinners/
        //      /Dinners/Page/2
        public ActionResult Index(int? page)
        {
            const int pageSize = 10;
            var upcomingDinners = dinnerRepository.FindUpcomingDinners();
            var paginatedDinners = new PaginatedList<Dinner>(upcomingDinners, page ?? 0, pageSize);
            return View(paginatedDinners);
        }
        //
        // GET: /Dinners/Details/2
        public ActionResult Details(int id)
        {
            Dinner dinner = dinnerRepository.GetDinner(id);
            if (dinner == null)
                return View("NotFound");
            else
                return View("Details", dinner);
        }
        //
        // GET: /Dinners/Edit/2
        [Authorize]
        public ActionResult Edit(int id)
        {
            Dinner dinner = dinnerRepository.GetDinner(id);
            if (!dinner.IsHostedBy(User.Identity.Name))
                return View("InvalidOwner");
            return View(new DinnerFormViewModel(dinner));
        }
        //
        // POST: /Dinners/Edit/2
        [Authorize]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            Dinner dinner = dinnerRepository.GetDinner(id);
            try
            {
                UpdateModel(dinner);
                dinnerRepository.Save();
                return RedirectToAction("Details", new { id = dinner.DinnerID });
            }
            catch
            {
                ModelState.AddRuleViolations(dinner.GetRuleViolations());
                //use view data
                //ViewData["countries"] = new SelectList(PhoneValidator.Countries, dinner.Country);
                //return View(dinner);
                //use strong data type
                return View(new DinnerFormViewModel(dinner));
            }
        }

        //
        // GET: /Dinners/Create
        public ActionResult Create()
        {
            Dinner dinner = new Dinner()
            {
                EventDate = DateTime.Now.AddDays(7)
            };

            return View(new DinnerFormViewModel(dinner));
        }
        //
        // POST: /Dinners/Create
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Dinner dinner)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    dinner.HostedBy = "SomeUser";
                    dinnerRepository.Add(dinner);
                    dinnerRepository.Save();
                    return RedirectToAction("Details", new { id = dinner.DinnerID });
                }
                catch
                {
                    ModelState.AddRuleViolations(dinner.GetRuleViolations());
                }
            }
            return View(new DinnerFormViewModel(dinner));
        }
        //
        // HTTP GET: /Dinners/Delete/1
        public ActionResult Delete(int id)
        {
            Dinner dinner = dinnerRepository.GetDinner(id);
            if (dinner == null)
                return View("NotFound");
            else
                return View(dinner);
        }
        //
        // HTTP POST: /Dinners/Delete/1
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, string confirmButton)
        {
            Dinner dinner = dinnerRepository.GetDinner(id);
            if (dinner == null)
                return View("NotFound");
            dinnerRepository.Delete(dinner);
            dinnerRepository.Save();
            return View("Deleted");
        }
    }//end class

    public static class ControllerHelpers
    {
        public static void AddRuleViolations(this ModelStateDictionary modelState, IEnumerable<RuleViolation> errors)
        {
            foreach (RuleViolation issue in errors)
            {
                modelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
            }
        }
    }
}
